﻿using Athena.Message;
using BeetleX.Buffers;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace Athena.Transport.BeetleX
{
    using Codec;
    public class BeetleXTransportMessageServerSender : ITransportMessageSender
    {
        private readonly PipeStream _pipeStream;
        private readonly IRpcMessageEncoder _rpcMessageEncoder;
        private readonly ILogger _logger;
        public BeetleXTransportMessageServerSender(PipeStream pipeStream, IRpcMessageEncoder rpcMessageEncoder, ILogger logger)
        {
            _pipeStream = pipeStream;
            _rpcMessageEncoder = rpcMessageEncoder;
            logger = _logger;
        }

        public async Task SendAndFlushAsync(RpcTransportMessage rpcTransportMessage)
        {
            await SendAsync(rpcTransportMessage);
            await _pipeStream.FlushAsync();
        }

        public Task SendAsync(RpcTransportMessage rpcTransportMessage)
        {
            var rpcTransportMessageString = _rpcMessageEncoder.MessageEncodingString(rpcTransportMessage);
            _pipeStream.WriteUTF(rpcTransportMessageString);
            return Task.CompletedTask;
        }
    }
}
